<?php
function loadAllTeams(& $races) {
    $teams = array ();
    $rs = executeQuery("select team_id from teams");
    while ($row = mysql_fetch_row($rs)) {
        $team = new Team();
        $team->noSeasonLoad($row[0]);
        $teams[$team->id] = $team;
    }

    return $teams;
}

function addNewTeam($coachName) {
    $rs = executeQuery("select max(team_id) from teams");
    $row = mysql_fetch_row($rs);
    $teamId = 1 + $row[0];
    $rs = executeQuery("select coach_id from coaches where name='$coachName'");
    $row = mysql_fetch_row($rs);
    $coachId = $row[0];
    $rs = executeQuery("insert into teams (team_id, fk_coach_id, name, roster) values ($teamId, $coachId, 'New Team', '???')");
    return $teamId;
}

/* Class file for a teams */
class Teams {
    var $season;
    var $teams;

    function Teams() {
        global $season;
        $this->season = & $season;
        $this->teams = array ();

        $rs = executeQuery("select fk_team_id from seasons_teams where fk_season_id=$season->id");
        while ($row = mysql_fetch_row($rs)) {
            $team = new Team();
            $teamId = $row[0];
            $team->load($teamId);
            $this->teams[$teamId] = $team;
        }
        uasort($this->teams, "leagueSort");
    }

    function numberOfTeams() {
        return sizeof($this->teams);
    }

    function getTeam($id) {
    	if (isset($id)) {
	    	if (isset($this->teams[$id])) {
	    	    return  $this->teams[$id];
			}
	        $team = new Team();
	        $team->noSeasonLoad($id);
	        return $team;
    	}
    	return false;
    	
    }

    function setTeam(& $newTeam) {
        $this->teams[$newTeam->id] = $newTeam;
    }

    function updateFullTeam($team, $team_name, $race, $treasury, $moneyLent, $moneyBorrowed, $rerolls, $rerollCost, $fanfactor, $coaches, $cheerleaders, $apothecary, $text_colour, $background_colour, $title_background_colour, $title_text_colour, $light_border, $dark_border) {

        $team->name = stripslashes($team_name);
        $team->race = $race;
        $team->treasury = strlen($treasury) == 0 ? 0 : $treasury;
        $team->moneyLent = strlen($moneyLent) == 0 ? 0 : $moneyLent;
        $team->moneyBorrowed = strlen($moneyBorrowed) == 0 ? 0 : $moneyBorrowed;
        $team->rerolls = strlen($rerolls) == 0 ? 0 : $rerolls;
        $team->fanFactor = strlen($fanfactor) == 0 ? 0 : $fanfactor;
        $team->coaches = strlen($coaches) == 0 ? 0 : $coaches;
        $team->cheerleaders = strlen($cheerleaders) == 0 ? 0 : $cheerleaders;
        $team->apothecary = strlen($apothecary) == 0 ? 0 : $apothecary;
        $team->text = $text_colour;
        $team->background = $background_colour;
        $team->titleBackground = $title_background_colour;
        $team->titleText = $title_text_colour;
        $team->borderLight = $light_border;
        $team->borderDark = $dark_border;

        $team->validateRosterNumbers();

        $this->setTeam($team);
    }

    function updateTeam($id, $name, $race, $team_rating, $roster, $roster_name, $url) {
        $team = $this->getTeam($id);
        $result = $team->update($name, $race, $team_rating, $roster, $roster_name, $url);
        $this->setTeam($team);

        uasort($this->teams, "leagueSort");
        return $result;
    }

    function addMatchResult($match) {
        if ($match->type == "League") {
            $team = $this->getTeam($match->homeRec->teamId);
            $team->addResult($match->homeRec, $match->awayRec, $match);
            $this->setTeam($team);

            $team = $this->getTeam($match->awayRec->teamId);
            $team->addResult($match->awayRec, $match->homeRec, $match);
            $this->setTeam($team);

            uasort($this->teams, "leagueSort");
        }
    }
}
?>